package com.asiainfo.dacp.datastash.domain.unit.models;

import com.asiainfo.dacp.datastash.domain.stepdefine.models.DataStashStepDefine;
import com.asiainfo.dacp.domain.Entity;
import com.asiainfo.dacp.util.JsonHelper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Transient;
import java.io.Serializable;
import java.util.*;

/**
 * Created by winter on 2017/10/31.
 */
@Data
public class DataStashUnitStep implements Entity,Serializable {

    private String id;

    private String unitId;

    private String unitCode;

    private String stepIndex;

    private String stepLabel;

    private String stepInst;

    private String preStepIdx;

    private String nextStepIdx;

    private String ignoreErrStepIdx;

    private String parentStepIdx;

    private String stepCfgVal;

    private String stepType;

    @JsonIgnore
    @Transient
    public List<DataStashUnitStep> nextSteps = new ArrayList<>();

    @JsonIgnore
    @Transient
    public List<DataStashUnitStep> preSteps = new ArrayList<>();

    @JsonIgnore
    @Transient
    public List<DataStashUnitStep> children = new ArrayList<>();//解析后的子步骤，对应在for循环，sharding分片框中

    @JsonIgnore
    @Transient
    public DataStashUnitStep parent;
    
//    private String groupType;

    private DataStashStepDefine stepDefine;

    public Map<String,Object> getStepCfgValMap() {
        if(stepCfgVal!=null && stepCfgVal.length()>0){
            return JsonHelper.getInstance().read(stepCfgVal,Map.class);
        }else{
            return new HashMap();
        }
    }


    public String getIgnoreErrStepIdx(){
        if(ignoreErrStepIdx==null){
            return "";
        }
        return ignoreErrStepIdx;
    }

    @JsonIgnore
    @Transient
    public List<String> getIgnoreErrStepIdxs(){
        if(StringUtils.isNotBlank(ignoreErrStepIdx)){
            return Arrays.asList(ignoreErrStepIdx.split(","));
        }
        return new ArrayList<String>();
    }

    public String getNextStepIdx(){
        if(nextStepIdx==null){
            return "";
        }
        return nextStepIdx;
    }

    @JsonIgnore
    @Transient
    public List<String> getNextStepIdxs(){
        if(StringUtils.isNotBlank(nextStepIdx)){
            return Arrays.asList(nextStepIdx.split(","));
        }
        return new ArrayList<String>();
    }

    public String getPreStepIdx(){
        if(preStepIdx==null){
            return "";
        }
        return preStepIdx;
    }

    @JsonIgnore
    @Transient
    public List<String> getPreStepIdxs(){
        if(StringUtils.isNotBlank(preStepIdx)){
            return Arrays.asList(preStepIdx.split(","));
        }
        return new ArrayList<String>();
    }
}
